package jun.learn.thread.deadlock;

public class DeadLockRisk {
	private class Resource{
		public int value;
	}
	
	private Resource ra = new Resource();
	private Resource rb = new Resource();
	
	public int read(){
		synchronized(ra){
			System.out.println("read() "+Thread.currentThread().getName()+" 获得了ra上的锁");
			synchronized (rb) {
				System.out.println("read() "+Thread.currentThread().getName()+" 获得了rb上的锁");
				return ra.value + rb.value;
			}
		}
	}
	
	
	public void write(int a,int b){
		synchronized(rb){
			System.out.println("read() "+Thread.currentThread().getName()+" 获得了rb上的锁");
			synchronized (ra) {
				System.out.println("read() "+Thread.currentThread().getName()+" 获得了ra上的锁");
				rb.value = b;
				ra.value = a;
			}
		}
	}
}
